export const common = {
	treating(data) {
		let jdata = JSON.parse(data.substr(1, data.length - 2));
		return jdata
	},
	// 判断字符串中是否包含数字
	containsNumber(str) {
		let reg = /\d/;
		return reg.test(str);
	},
	fun_hint(e) {
		uni.showToast({
			title: e,
			icon: 'none',
			duration: 2000
		});
	},
	previewD(url) {
		const imgList = [url];
		uni.previewImage({
			urls: imgList
		});
	},
	/**
	 * @param {Stringg} str 需要加解密的字符
	 * @param {String} code 类型,加密(encode)还是解密(decode)，
	 * @returns 加密/解密后的字符串
	 */
	Base64(str, code) {
		// 私钥   A-Z(a-z) 每个要写全,每个字母可以重复
		let dc__key_Str = "AABBBCCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		// 加密
		const encode = (input) => {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
			input = _utf8_encode(input);
			while (i < input.length) {
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
				if (isNaN(chr2)) {
					enc3 = enc4 = 64;
				} else if (isNaN(chr3)) {
					enc4 = 64;
				}
				output = output + dc__key_Str.charAt(enc1) + dc__key_Str.charAt(enc2) +
					dc__key_Str.charAt(enc3) + dc__key_Str.charAt(enc4);
			}
			return output;
		}
		// 解密
		const decode = (input) => {
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
			if (input == undefined || input == null) {} else {
				input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
				while (i < input.length) {
					enc1 = dc__key_Str.indexOf(input.charAt(i++));
					enc2 = dc__key_Str.indexOf(input.charAt(i++));
					enc3 = dc__key_Str.indexOf(input.charAt(i++));
					enc4 = dc__key_Str.indexOf(input.charAt(i++));
					chr1 = (enc1 << 2) | (enc2 >> 4);
					chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
					chr3 = ((enc3 & 3) << 6) | enc4;
					output = output + String.fromCharCode(chr1);
					if (enc3 != 64) {
						output = output + String.fromCharCode(chr2);
					}
					if (enc4 != 64) {
						output = output + String.fromCharCode(chr3);
					}
				}
				output = _utf8_decode(output);
				return output;
			}

		}
		// private method for UTF-8 encoding
		let _utf8_encode = (string) => {
			string = string.replace(/\r\n/g, "\n");
			var utftext = "";
			for (var n = 0; n < string.length; n++) {
				var c = string.charCodeAt(n);
				if (c < 128) {
					utftext += String.fromCharCode(c);
				} else if ((c > 127) && (c < 2048)) {
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				} else {
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
			}
			return utftext;
		}
		// private method for UTF-8 decoding
		let _utf8_decode = (utftext) => {
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
			var c1 = 0;
			var c2 = 0;
			var c3 = 0;
			while (i < utftext.length) {
				c = utftext.charCodeAt(i);
				if (c < 128) {
					string += String.fromCharCode(c);
					i++;
				} else if ((c > 191) && (c < 224)) {
					c2 = utftext.charCodeAt(i + 1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				} else {
					c2 = utftext.charCodeAt(i + 1);
					c3 = utftext.charCodeAt(i + 2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
			}
			return string;
		}
		//加密
		if (code == 'encode') {
			return encode(str)
		}
		//解密
		if (code == 'decode') {
			return decode(str)
		}
	},

	findHighFrequencyItems(dataArray, splitCount) {
		if (!Array.isArray(dataArray) || dataArray.length === 0) {
			throw new Error('第一个参数必须是非空数组');
		}

		if (typeof splitCount !== 'number' || splitCount <= 0) {
			throw new Error('第二个参数必须是正整数');
		}

		// 1. 拆分数组为前后两部分
		const frontParts = [];
		const backParts = [];

		dataArray.forEach(item => {
			if (!Array.isArray(item)) {
				throw new Error('二维数组的每个元素都必须是数组');
			}

			if (item.length <= splitCount) {
				throw new Error(`子数组长度必须大于拆分位数 ${splitCount}`);
			}

			const frontPart = item.slice(0, -splitCount);
			const backPart = item.slice(-splitCount);

			frontParts.push(frontPart);
			backParts.push(backPart);
		});

		// 2. 统计前后部分的数字频率
		const frontFrequency = {};
		const backFrequency = {};

		// 统计前部分频率
		frontParts.flat().forEach(num => {
			frontFrequency[num] = (frontFrequency[num] || 0) + 1;
		});

		// 统计后部分频率
		backParts.flat().forEach(num => {
			backFrequency[num] = (backFrequency[num] || 0) + 1;
		});

		// 3. 找出高频数字（出现次数最多的数字）
		const getHighFrequencyNumbers = (frequencyObj) => {
			const maxFrequency = Math.max(...Object.values(frequencyObj));
			return Object.keys(frequencyObj)
				.filter(key => frequencyObj[key] === maxFrequency)
				.map(Number);
		};

		const highFreqFront = getHighFrequencyNumbers(frontFrequency);
		const highFreqBack = getHighFrequencyNumbers(backFrequency);

		// 4. 计算每个原始项包含高频数字的得分
		const scores = dataArray.map((item, index) => {
			const frontPart = frontParts[index];
			const backPart = backParts[index];

			// 计算前部分得分：包含的高频数字数量
			const frontScore = frontPart.filter(num =>
				highFreqFront.includes(num)
			).length;

			// 计算后部分得分：包含的高频数字数量
			const backScore = backPart.filter(num =>
				highFreqBack.includes(num)
			).length;

			return {
				/** 原始数组 */
				originalItem: item,
				/** 前部分*/
				frontPart: frontPart,
				/** 前部分得分*/
				frontScore: frontScore,
				/** 后部分*/
				backPart: backPart,
				/** 后部分得分*/
				backScore: backScore,
				/** 总得分  */
				totalScore: frontScore + backScore
			};
		});

		// 5. 找出得分最高的项
		const maxScore = Math.max(...scores.map(score => score.totalScore));
		const highestScoreItems = scores.filter(score => score.totalScore === maxScore);

		// 返回结果对象
		return {
			/** 高频数字统计 */
			frequencyStats: {
				front: frontFrequency,
				back: backFrequency,
				highFrequencyFront: highFreqFront,
				highFrequencyBack: highFreqBack
			},
			/** 所有项的得分详情 */
			allScores: scores,
			/** 最高分的项（可能有多个） */
			highestScoreItems: highestScoreItems,
			/** 如果只有一个最高分项，直接返回该项 */
			result: highestScoreItems.length === 1 ? highestScoreItems[0].originalItem : highestScoreItems.map(
				item => item.originalItem)
		};
	},
	//  简化版本（如果只需要最终结果）
	simpleFindHighFrequency(dataArray, splitCount) {
		const result = this.findHighFrequencyItems(dataArray, splitCount);
		return Array.isArray(result.result) ?
			(!Array.isArray(result.result[0]) ? result.result : result.result[0]) :
			result.result;
	},


}